package com.QK.cnstudy.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.QK.cnstudy.R;
import com.QK.cnstudy.ResultActivity;

public class ResultAdapter extends QKAdapter {

    private int titleResource = R.layout.result_title;
    private int wrongResource = R.layout.result_wrong;
    private int naviResource = R.layout.result_navi;
    public Handler handler;
    public final static int FLAG_TITLE = 0;
    public final static int FLAG_WRONG = 1;
    public final static int FLAG_NAVI = 2;

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    public ResultAdapter(Context context, List<Map<String, Object>> list, Handler handler) {
        super(context, list);
        this.handler = handler;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        Map<String, Object> map = list.get(position);

        ViewHolder holder = null;
        switch ((Integer) map.get("flag")) {
        case FLAG_TITLE:
            if (convertView == null || ((ViewHolder) convertView.getTag()).flag != FLAG_TITLE) {
                holder = new ViewHolder();
                convertView = inflater.inflate(titleResource, null);
                holder.keyTextView = (TextView) convertView.findViewById(R.id.key);
                holder.valueTextView = (TextView) convertView.findViewById(R.id.value);
                holder.flag = FLAG_TITLE;
                convertView.setTag(holder);
            }
            holder = (ViewHolder) convertView.getTag();
            String keyString = (String) map.get("key");
            String valueString = (String) map.get("value");
            holder.keyTextView.setText(keyString);
            holder.valueTextView.setText(valueString);
            break;
        case FLAG_WRONG:
            if (convertView == null || ((ViewHolder) convertView.getTag()).flag != FLAG_WRONG) {
                holder = new ViewHolder();
                convertView = inflater.inflate(wrongResource, null);
                holder.contentTextView = (TextView) convertView.findViewById(R.id.content);
                holder.translateTextView = (TextView) convertView.findViewById(R.id.translate);
                holder.flag = FLAG_WRONG;
                convertView.setTag(holder);
            }
            holder = (ViewHolder) convertView.getTag();
            String contentString = (String) map.get("content");
            String translateString = (String) map.get("translate");
            holder.contentTextView.setText(contentString);
            holder.translateTextView.setText(translateString);
            break;
        case FLAG_NAVI:
            if (convertView == null || ((ViewHolder) convertView.getTag()).flag != FLAG_NAVI) {
                holder = new ViewHolder();
                convertView = inflater.inflate(naviResource, null);
                holder.againButton = (Button) convertView.findViewById(R.id.again);
                holder.finishButton = (Button) convertView.findViewById(R.id.finish);

                holder.againButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        handler.sendEmptyMessage(ResultActivity.MSG_ADDWRONG);
                    }
                });
                holder.flag = FLAG_NAVI;
                convertView.setTag(holder);
            }
            holder = (ViewHolder) convertView.getTag();
            break;
        }
        return convertView;
    }

    class ViewHolder {
        public int flag;
        public TextView keyTextView;
        public TextView valueTextView;

        public TextView contentTextView;
        public TextView translateTextView;
        public Button againButton;
        public Button finishButton;

    }

}
